package tech.aistar.prj.controller;

import tech.aistar.prj.model.dto.VideoDTO;
import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.service.IVideoService;
import tech.aistar.prj.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = "/video/*")
public class IndexController extends HttpServlet {
    private IVideoService videoService = new VideoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<VideoDTO> videoList = null;

        req.setCharacterEncoding("utf-8");
        //从session作用域获取
        User user = (User) req.getSession().getAttribute("user");

        String pathInfo = req.getPathInfo();

        switch (pathInfo){
            case "/index":
                Double scoreNum = null;
                Integer likesNum = null;
                String username = req.getParameter("username");
                username = (username==null?username:username.trim());
                String score = req.getParameter("score");
                if(null!=score && score.trim().length()>0){
                    scoreNum = (Double.valueOf(score));
                }
                String likes = req.getParameter("likes");
                if(null!=likes && likes.trim().length()>0){
                    likesNum = Integer.valueOf(likes);
                }
                String title = req.getParameter("title");
                title = (title==null?title:title.trim());

                String sort = req.getParameter("sort");
                sort = (sort==null?sort:sort.trim());
                System.out.println(sort);
                //删除
                String delId = req.getParameter("delId");
                if (null!=delId && delId.trim().length()>0){
                    videoService.delVideo(Integer.valueOf(delId));
                }
                Map<String,String> dataMap = new HashMap<>();
                dataMap.put("username",username==null?"":username.trim());
                dataMap.put("title",title==null?"":title.trim());
                dataMap.put("likes",likes==null?"":likes.trim());
                dataMap.put("score",score==null?"":score.trim());
                req.setAttribute("dataMap",dataMap);

                System.out.println("rise".equals(sort));

                videoList = videoService.findByConditions(username,scoreNum,likesNum,title);

                if ("rise".equals(sort)){
                    videoList = videoService.rise(videoList);

                }else if ("desc".equals(sort)){
                    videoList = videoService.desc(videoList);
                }
                req.setAttribute("videoList",videoList);
                req.getRequestDispatcher("/nav/indexView").forward(req,resp);
                break;
            case "/personal":
                String delIdMy = req.getParameter("delId");
                String releaseId = req.getParameter("releaseId");
                if (null!=delIdMy && delIdMy.trim().length()>0){
                    videoService.delVideo(Integer.valueOf(delIdMy));
                }
                if (null!=releaseId && releaseId.trim().length()>0){
                    videoService.releaseVideo(Integer.valueOf(releaseId));
                }
                List<Video> videos = videoService.searchByUser(user.getUsername());
                videos = videoService.descByStatusAndTime(videos);
                req.setAttribute("videos",videos);
                //转发到我的发布的视频页面
                req.getRequestDispatcher("/nav/personalView").forward(req,resp);
                break;
            case "/release":
                //获取标题
                String t = req.getParameter("title");
                String videoUrl = req.getParameter("videoUrl");
                String like = req.getParameter("likes");
                String scores = req.getParameter("score");
                String status = req.getParameter("status");
                Video v = new Video();
                v.setTitle(t);
                v.setVideoUrl(videoUrl);
                v.setLikes(Integer.valueOf(like));
                v.setScore(Double.valueOf(scores));
                v.setStatus(Integer.valueOf(status));
                v.setUserId(user.getId());
                v.setCreateDate(new Date());
                videoService.addVideo(v);
                //一旦保存成功之后 - 跳转到我的发布中去
                resp.sendRedirect("/servlet/video/personal");
                break;
            case "/update":
                String upId = req.getParameter("upId");
                //查询对应视频信息将信息给到updateView
                Video video = videoService.findByVid(Integer.valueOf(upId));
                req.setAttribute("video",video);

                req.getRequestDispatcher("/nav/updateView").forward(req,resp);
                break;
            case "/updateGet":
                String uId = req.getParameter("vId");
                String ut = req.getParameter("title");
                String uVideoUrl = req.getParameter("uVideoUrl");
                String uLike = req.getParameter("likes");
                String uScores = req.getParameter("score");
                String uStatus = req.getParameter("status");

                videoService.updateVideo(ut,uVideoUrl,Integer.valueOf(uLike),Double.valueOf(uScores),Integer.valueOf(uStatus),Integer.valueOf(uId));

                resp.sendRedirect("/servlet/video/personal");
                break;
        }
    }
}
